class UiManager extends BaseClass {
	public indexList:Array<any> = [];
	public constructor() {
		super();
		this.addEventListener();
	}
	public addEventListener() {
		App.EventManager.addEventListener(GameConstants.UI_ADD_TO_PARENT, this.addToParent, this);
		App.EventManager.addEventListener(GameConstants.UI_REMOVE_FROM_PARENT, this.removeFromeParent, this);
	}
	private removeFromeParent(index: any) {
		App.EventManager.dispatchEvent(GameConstants.UI_WILL_DISAPPEAR,index);
		 let indexList = [];
		 for(let i=0;i<this.indexList.length;i++){
			 if(index != this.indexList[i]){
				 indexList.push(this.indexList[i]);
			 }
		 }
		this.indexList = indexList;
		switch (index) {
			case GameConfig.uiIndex.load:
				this.removeScene(gameUi.loadScene);
				gameUi.loadScene = null;
				break;
			case GameConfig.uiIndex.home:
				this.removeScene(gameUi.homeScene);
				gameUi.homeScene = null;
				break;
			case GameConfig.uiIndex.gameReborn:
				this.removeScene(gameUi.gameRebornPanel);
				gameUi.gameRebornPanel = null;
				break;
			case GameConfig.uiIndex.game:
				this.removeScene(gameUi.gameScene);
				gameUi.gameScene = null;
				break;
			case GameConfig.uiIndex.rankListFromHome:
				this.removeScene(gameUi.rankListPanel);
				gameUi.rankListPanel = null;
				break;
			case GameConfig.uiIndex.rankListFromOver:
				this.removeScene(gameUi.rankListPanel);
				gameUi.rankListPanel = null;
				break;
			case GameConfig.uiIndex.over:
				this.removeScene(gameUi.gameOverScene);
				gameUi.gameOverScene = null;
				break;
			case GameConfig.uiIndex.groupRankList:
				this.removeScene(gameUi.groupRankListPanel);
				gameUi.gameOverScene = null;
				break;
			case GameConfig.uiIndex.pause:
				this.removeScene(gameUi.gamePausePanel);
				gameUi.gamePausePanel = null;
				break;
			case GameConfig.uiIndex.redpacket:
				this.removeScene(gameUi.redpacketPanel);
				gameUi.redpacketPanel = null;
				break;
				
		}
		if(this.indexList &&this.indexList.length>0){
			let willHideIndex = this.indexList[this.indexList.length-1];
			App.EventManager.dispatchEvent(GameConstants.UI_WILL_APPEAR,willHideIndex);
		}
	}
	private removeScene(scene:egret.DisplayObject)
	{
		if(scene && scene.parent){
			scene.parent.removeChild(scene);
		}
	}
	private addToParent(index: any) {
		if(this.indexList &&this.indexList.length>0){
			let willHideIndex = this.indexList[this.indexList.length-1];
			App.EventManager.dispatchEvent(GameConstants.UI_WILL_DISAPPEAR,willHideIndex);
		}
		
		this.indexList.push(index);
		switch (index) {
			case GameConfig.uiIndex.load:
				gameUi.loadScene = new LoadingScene();
				gameUi.loadScene.addToParent();
				break;
			case GameConfig.uiIndex.home:
				gameUi.homeScene = new HomeScene();
				gameUi.homeScene.addToParent();
				break;
			case GameConfig.uiIndex.game:
				gameUi.gameScene = new GameScene();
				gameUi.gameScene.addToParent();
				break;
			case GameConfig.uiIndex.gameReborn:
				gameUi.gameRebornPanel = new GameRebornPanel();
				gameUi.gameRebornPanel.addToParent();
				break;
			case GameConfig.uiIndex.over:
				gameUi.gameOverScene = new GameOverScene();
				gameUi.gameOverScene.addToParent();
				break;
			case GameConfig.uiIndex.rankListFromOver:
				gameUi.rankListPanel = new RankListPanel(GameConfig.uiIndex.rankListFromOver);
				gameUi.rankListPanel.addToParent();
				break;
			case GameConfig.uiIndex.rankListFromHome:
				gameUi.rankListPanel = new RankListPanel(GameConfig.uiIndex.rankListFromHome);
				gameUi.rankListPanel.addToParent();
				break;
			case GameConfig.uiIndex.groupRankList:
				gameUi.groupRankListPanel = new GroupRankListPanel();
				gameUi.groupRankListPanel.addToParent();
				break;
			case GameConfig.uiIndex.pause:
				gameUi.gamePausePanel = new GamePausePanel();
				gameUi.gamePausePanel.addToParent();
				break;
			case GameConfig.uiIndex.redpacket:
				gameUi.redpacketPanel = new RedPacketPanel();
				gameUi.redpacketPanel.addToParent();
				break;
		}
		App.EventManager.dispatchEvent(GameConstants.UI_WILL_APPEAR,index);
	}
}